﻿using Fofia.ObjectPool;
using IoTSharp.Data.Taos;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Test
{
    public class TaosDbFactory
    {
        private static ObjectPool<TaosConnection> _pool;
        static TaosDbFactory()
        {

        }

        public static void Init()
        {
            string connectionString = "Data Source=192.168.1.211;DataBase=iot_tags;Username=root;Password=taosdata;Port=6030";
            //_pool = new ObjectPool<TaosConnection>(0, 0,0,()=> new TaosConnection(connectionString));

            var option = new PoolOption<TaosConnection>();
            option.ObjectGenerator = () => new TaosConnection(connectionString);
            _pool = new ObjectPool<TaosConnection>(option);

        }

        public TaosConnection InitConn()
        {
            string connectionString = "Data Source=192.168.1.211;DataBase=iot_tags;Username=root;Password=taosdata;Port=6030";
            return new TaosConnection(connectionString);
        }
        public static TaosConnection GetTaos()
        {
            var taos = _pool.Get();

           //WeakReference weakRef = new WeakReference(taos);
            return taos;
        }


        public static void Return(TaosConnection connection)
        {
            _pool.Return(connection);
        }
    }
}
